﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DBService;
using YJKArch.ImportStructureProject.ImportObjects;
using YJKArch.Utils;

namespace YJKArch.ImportStructureProject
{
	internal class TestUploadStructureData
	{
		public TestUploadStructureData(ExternalCommandData commandData)
		{
			this.m_Revit = commandData;
		}

		public bool Upload(ref string errMessage)
		{
			this.m_Db = BimServerDB.Default;
			DbProject projectByCurrentDoc = DbProject.GetProjectByCurrentDoc(this.m_Revit.Application.ActiveUIDocument.Document);
			this.m_ProjectId = projectByCurrentDoc.Id;
			try
			{
				this.ClearOldStructureInfo(projectByCurrentDoc);
			}
			catch (Exception)
			{
				errMessage = "清空数据库旧数据出错。";
				return false;
			}
			if (!this.ReadStructureInfo(ref errMessage))
			{
				return false;
			}
			this.UploadStructureInfo();
			return true;
		}

		private void ClearOldStructureInfo(DbProject curProject)
		{
			DbBeam.Clear(curProject);
			DbColumn.Clear(curProject);
			DbWall.Clear(curProject);
			DbSlab.Clear(curProject);
			DbElementForRemove.Clear(curProject);
		}

		private bool ReadStructureInfo(ref string errMessage)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "输人XML文件(*.xml)|*.xml";
			if (openFileDialog.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			string structureInfoFilePath = openFileDialog.FileName.ToString();
			return new ReadStructureXMLFile(ref this.m_OldColumnsId, ref this.m_OldBeamsId, ref this.m_OldWallsId, ref this.m_OldFloorsId, ref this.m_BeamStructureData, ref this.m_ColumnStructureData, ref this.m_WallStructureData, ref this.m_FloorStructureData).ReadStructureInfo(structureInfoFilePath, ref errMessage);
		}

		private void UploadStructureInfo()
		{
			List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
			int num = 0;
			KeyValuePair<int, string> item = new KeyValuePair<int, string>(this.m_BeamStructureData.Count, "梁");
			list.Add(item);
			num += item.Key;
			KeyValuePair<int, string> item2 = new KeyValuePair<int, string>(this.m_ColumnStructureData.Count, "柱");
			list.Add(item2);
			num += item2.Key;
			KeyValuePair<int, string> item3 = new KeyValuePair<int, string>(this.m_WallStructureData.Count, "墙");
			list.Add(item3);
			num += item3.Key;
			KeyValuePair<int, string> item4 = new KeyValuePair<int, string>(this.m_FloorStructureData.Count, "楼板");
			list.Add(item4);
			num += item4.Key;
			MyProgressBar myProgressBar = new MyProgressBar(list, "上传结构数据", true);
			this.UploadBeam(ref myProgressBar);
			this.UploadColumn(ref myProgressBar);
			this.UploadWall(ref myProgressBar);
			this.UploadFloor(ref myProgressBar);
			this.UploadRemoveElements();
			myProgressBar.Dispose();
		}

		private void UploadBeam(ref MyProgressBar myProgress)
		{
			foreach (BeamInfos beamInfos in this.m_BeamStructureData)
			{
				SectionParameter sParam;
				if (beamInfos.SectionStyle == PoleSectionStyle.CSS_RECTANG)
				{
					sParam = new SectionParameter(beamInfos.Width, beamInfos.Height);
				}
				else
				{
					sParam = new SectionParameter(beamInfos.Width);
				}
				int racId = 0;
				int rstId = 0;
				if (null != beamInfos.OldElementId)
				{
					racId = beamInfos.OldElementId.IntegerValue;
				}
				new DbBeam(this.m_ProjectId, racId, rstId, beamInfos.StartPoint, beamInfos.EndPoint, beamInfos.Bulge, sParam).AddToDb(this.m_Db);
				if (!myProgress.ProgressStep(1) && myProgress.IsStoped)
				{
					break;
				}
			}
		}

		private void UploadColumn(ref MyProgressBar myProgress)
		{
			foreach (ColumnInfos columnInfos in this.m_ColumnStructureData)
			{
				SectionParameter sParam;
				if (columnInfos.SectionStyle == PoleSectionStyle.CSS_RECTANG)
				{
					sParam = new SectionParameter(columnInfos.H, columnInfos.B);
				}
				else
				{
					sParam = new SectionParameter(columnInfos.H);
				}
				int racId = 0;
				int rstId = 0;
				if (null != columnInfos.OldElementId)
				{
					racId = columnInfos.OldElementId.IntegerValue;
				}
				new DbColumn(this.m_ProjectId, racId, rstId, columnInfos.LocationPoint, columnInfos.Height, columnInfos.Rotation, sParam).AddToDb(this.m_Db);
				if (!myProgress.ProgressStep(1) && myProgress.IsStoped)
				{
					break;
				}
			}
		}

		private void UploadWall(ref MyProgressBar myProgress)
		{
			foreach (WallInfos wallInfos in this.m_WallStructureData)
			{
				List<DbOpening> list = new List<DbOpening>();
				foreach (WallHoleInfos wallHoleInfos in wallInfos.Holes)
				{
					List<DbEdge> list2 = new List<DbEdge>();
					DbEdge item = new DbEdge(wallHoleInfos.Point1, wallHoleInfos.Point2, 0.0);
					list2.Add(item);
					DbEdgeLoop del = new DbEdgeLoop(list2);
					int racId = 0;
					int modleId = 0;
					if (null != wallHoleInfos.OldElementId)
					{
						racId = wallHoleInfos.OldElementId.IntegerValue;
					}
					DbOpening item2 = new DbOpening(this.m_ProjectId, del, racId, modleId, OpeningType.Rst);
					list.Add(item2);
				}
				int racId2 = 0;
				int rstId = 0;
				if (null != wallInfos.OldElementId)
				{
					racId2 = wallInfos.OldElementId.IntegerValue;
				}
				new DbWall(this.m_ProjectId, racId2, rstId, wallInfos.StartPoint, wallInfos.EndPoint, wallInfos.Bulge, wallInfos.Thickness, wallInfos.Height, list).AddToDb(this.m_Db);
				if (!myProgress.ProgressStep(1) && myProgress.IsStoped)
				{
					break;
				}
			}
		}

		private void UploadFloor(ref MyProgressBar myProgress)
		{
			foreach (FloorInfos floorInfos in this.m_FloorStructureData)
			{
				List<DbOpening> list = new List<DbOpening>();
				foreach (FloorHoleInfos floorHoleInfos in floorInfos.Holes)
				{
					List<DbEdge> list2 = new List<DbEdge>();
					foreach (EdgeInfos edgeInfos in floorHoleInfos.LoopEdges)
					{
						DbEdge item = new DbEdge(edgeInfos.StartPoint, edgeInfos.EndPoint, edgeInfos.Bulge);
						list2.Add(item);
					}
					DbEdgeLoop del = new DbEdgeLoop(list2);
					int racId = 0;
					int modleId = 0;
					if (null != floorHoleInfos.OldElementId)
					{
						racId = floorHoleInfos.OldElementId.IntegerValue;
					}
					DbOpening item2 = new DbOpening(this.m_ProjectId, del, racId, modleId, OpeningType.Rst);
					list.Add(item2);
				}
				List<DbEdge> list3 = new List<DbEdge>();
				foreach (EdgeInfos edgeInfos2 in floorInfos.LoopEdges)
				{
					DbEdge item3 = new DbEdge(edgeInfos2.StartPoint, edgeInfos2.EndPoint, edgeInfos2.Bulge);
					list3.Add(item3);
				}
				DbEdgeLoop edgeLoop = new DbEdgeLoop(list3);
				int racId2 = 0;
				int rstId = 0;
				if (null != floorInfos.OldElementId)
				{
					racId2 = floorInfos.OldElementId.IntegerValue;
				}
				new DbSlab(this.m_ProjectId, racId2, rstId, floorInfos.Thickness, edgeLoop, list).AddToDb(this.m_Db);
				if (!myProgress.ProgressStep(1) && myProgress.IsStoped)
				{
					break;
				}
			}
		}

		private void UploadRemoveElements()
		{
			foreach (ElementId elementId in this.m_OldBeamsId)
			{
                new DbElementForRemove(this.m_ProjectId, elementId.IntegerValue, DBService.ElementType.Rst, ElementCategory.Beam).AddToDb(this.m_Db);
			}
			foreach (ElementId elementId2 in this.m_OldColumnsId)
			{
                new DbElementForRemove(this.m_ProjectId, elementId2.IntegerValue, DBService.ElementType.Rst, ElementCategory.Column).AddToDb(this.m_Db);
			}
			foreach (ElementId elementId3 in this.m_OldWallsId)
			{
                new DbElementForRemove(this.m_ProjectId, elementId3.IntegerValue, DBService.ElementType.Rst, ElementCategory.Wall).AddToDb(this.m_Db);
			}
			foreach (ElementId elementId4 in this.m_OldFloorsId)
			{
                new DbElementForRemove(this.m_ProjectId, elementId4.IntegerValue, DBService.ElementType.Rst, ElementCategory.Slab).AddToDb(this.m_Db);
			}
		}

		private ExternalCommandData m_Revit;

		private BimServerDB m_Db;

		private int m_ProjectId;

		private List<ElementId> m_OldColumnsId = new List<ElementId>();

		private List<ElementId> m_OldBeamsId = new List<ElementId>();

		private List<ElementId> m_OldWallsId = new List<ElementId>();

		private List<ElementId> m_OldFloorsId = new List<ElementId>();

		private List<BeamInfos> m_BeamStructureData = new List<BeamInfos>();

		private List<ColumnInfos> m_ColumnStructureData = new List<ColumnInfos>();

		private List<WallInfos> m_WallStructureData = new List<WallInfos>();

		private List<FloorInfos> m_FloorStructureData = new List<FloorInfos>();
	}
}
